package com.jh.paymentchannel.service;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.jh.paymentchannel.util.Util;
import com.jh.paymentchannel.util.yeepay.AESUtil;
import com.jh.paymentchannel.util.yeepay.Conts;
import com.jh.paymentchannel.util.yeepay.Digest;
import com.jh.paymentchannel.util.yeepay.Img2Small;

import cn.jh.common.utils.CommonConstants;
import net.sf.json.JSONObject;

/**
 * 易宝交易
 *
 * @author ycl
 * @create 2017-10-10-上午10:20
 */
@Service
public class YBTopupService implements TopupRequest {

    private static final Logger log = LoggerFactory.getLogger(YBTopupService.class);
    //	private String realnamePic = "/usr/share/nginx/photo";
    private String realnamePic = "/product/upload/images/123";
    private String writeNamePic = "/usr/share/nginx/photo";
    private static String key = Conts.hmacKey; // 商户秘钥

    @Autowired
    Util util;

    @Override
    public Map<String, String> topupRequest(HttpServletRequest request, String amount, String ordercode,
                                            String orderdesc, String extra, String notifyurl, String returnurl, String channelParam) throws Exception {

        Map maps = new HashMap();
        RestTemplate restTemplate = new RestTemplate();
        URI uri = util.getServiceUrl("transactionclear", "error url request!");
        String url = uri.toString() + "/v1.0/transactionclear/payment/query/ordercode";
        MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
        requestEntity.add("order_code", ordercode);
        String result;
        JSONObject jsonObject;
        JSONObject resultObj = null;
        try {
            result = restTemplate.postForObject(url, requestEntity, String.class);
            log.info("接口/v1.0/transactionclear/payment/query/ordercode--RESULT================" + result);
            jsonObject = JSONObject.fromObject(result);
            resultObj = jsonObject.getJSONObject("result");
        } catch (Exception e) {
            log.error("查询订单有误=====" + e);
            maps.put(CommonConstants.RESP_CODE, "failed");
            maps.put(CommonConstants.RESP_MESSAGE, "查询订单信息有误");
            return maps;
        }
        String bankcard = resultObj.getString("bankcard");
        String channelid = resultObj.getString("channelid");
        String telphone = resultObj.getString("phone");
        String extraFee = resultObj.getString("extraFee");
        if (extraFee.contains("."))
            extraFee = extraFee.substring(0, extraFee.indexOf("."));
        /** 根据订单号获取相应的订单 */
        uri = util.getServiceUrl("user", "error url request!");
        url = uri.toString() + "/v1.0/user/bank/default/cardno";
        requestEntity = new LinkedMultiValueMap<String, String>();
        requestEntity.add("cardno", bankcard);
        requestEntity.add("type", "0");
        try {
            result = restTemplate.postForObject(url, requestEntity, String.class);
            log.info("接口/v1.0/user/bank/default/cardno--RESULT================" + result);
            jsonObject = JSONObject.fromObject(result);
            resultObj = jsonObject.getJSONObject("result");
        } catch (Exception e) {
            log.error("查询用户的信用卡有误======" + e);
            maps.put(CommonConstants.RESP_CODE, "failed");
            maps.put(CommonConstants.RESP_MESSAGE, "查询用户的信用卡出错啦");
            return maps;
        }
        String idcard = resultObj.getString("idcard");// 身份证号
        String userName = resultObj.getString("userName");// 用户姓名
        long userid = resultObj.getLong("userId");// 用户ID

        restTemplate = new RestTemplate();
        uri = util.getServiceUrl("user", "error url request!");
        url = uri.toString() + "/v1.0/user/bank/default/userid";
        requestEntity = new LinkedMultiValueMap<String, String>();
        requestEntity.add("user_id", String.valueOf(userid));
        result = restTemplate.postForObject(url, requestEntity, String.class);
        String respCode = "";
        log.info("接口/v1.0/user/bank/default/userid--RESULT================" + result);
        try {
            jsonObject = JSONObject.fromObject(result);
            resultObj = jsonObject.getJSONObject("result");
            respCode = jsonObject.getString("resp_code");
        } catch (Exception e1) {
            log.error("查询用户默认结算卡异常" + e1.getMessage());
            Map map = new HashMap();
            map.put("resp_code", "failed");
            map.put("resp_message", "没有结算卡");
            return map;
        }

        if (!"000000".equals(respCode)) {
            Map map = new HashMap();
            map.put("resp_code", "failed");
            map.put("resp_message", "没有结算卡");
            return map;
        }

//		if (null == resultObj || resultObj.isNullObject() || resultObj.isEmpty()) {
//			Map map = new HashMap();
//			map.put("resp_code", "failed");
//			map.put("resp_message", "没有结算卡");
//			return map;
//		}
//		if (null == jsonObject.get("result")) {
//			Map map = new HashMap();
//			map.put("resp_code", "failed");
//			map.put("resp_message", "没有结算卡");
//			return map;
//		}
        String bankNameJJ = resultObj.getString("bankName");// 借记卡银行名
        if ("平安银行".equals(bankNameJJ))
            bankNameJJ = "深圳发展银行";
        String cardNo = resultObj.getString("cardNo");// 借记卡卡号
        String province = resultObj.getString("province");
        String city = resultObj.getString("city");
        String areano = null;
        if (!"null".equals(province) && null != province && !"".equals(province)) {
            uri = util.getServiceUrl("paymentchannel", "error url request!");
            String road = uri.toString() + "/v1.0/paymentchannel/areanumber/queryareano";
            requestEntity = new LinkedMultiValueMap<String, String>();
            requestEntity.add("province", province);
            requestEntity.add("city", city);
            restTemplate = new RestTemplate();
            try {
                String resultObjx = restTemplate.postForObject(road, requestEntity, String.class);
                log.info("接口/v1.0/paymentchannel/areanumber/queryareano--RESULT================" + resultObjx);
                jsonObject = JSONObject.fromObject(resultObjx);
                areano = jsonObject.getString("result");
            } catch (Exception e) {
                log.error("查询区号有误=====" + e);
                maps.put(CommonConstants.RESP_CODE, "failed");
                maps.put(CommonConstants.RESP_MESSAGE, "查询区号信息出错啦");
                return maps;
            }
            // areano = resultObj.getString("areano");
        }
        uri = util.getServiceUrl("user", "error url request!");
        String road = uri.toString() + "/v1.0/user/channel/rate/query/userid";

        /** 根据的渠道标识或去渠道的相关信息 */
        requestEntity = new LinkedMultiValueMap<String, String>();
        requestEntity.add("channel_id", channelid);
        requestEntity.add("user_id", String.valueOf(userid));
        restTemplate = new RestTemplate();
        String resultObjx;
        try {
            resultObjx = restTemplate.postForObject(road, requestEntity, String.class);
            log.info("接口/v1.0/user/channel/rate/query/userid--RESULT================" + resultObjx);
            jsonObject = JSONObject.fromObject(resultObjx);
        } catch (Exception e) {
            log.error("查询费率信息有误======" + e);
            maps.put(CommonConstants.RESP_CODE, "failed");
            maps.put(CommonConstants.RESP_MESSAGE, "查询通道费率出错啦");
            return maps;
        }

        if (!CommonConstants.SUCCESS.equals(jsonObject.getString(CommonConstants.RESP_CODE))) {
            Map<String, String> map = new HashMap<String, String>();
            map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
            map.put(CommonConstants.RESP_MESSAGE, "亲.支付失败,臣妾已经尽力了,请重试!");
            return map;
        }

        resultObj = jsonObject.getJSONObject("result");
        String rate = resultObj.getString("rate");
        String rateCopy = rate;
        int withdrawFed = resultObj.getInt("withdrawFee");
        String withdrawFee = String.valueOf(withdrawFed);
        uri = util.getServiceUrl("paymentchannel", "error url request!");
        url = uri.toString() + "/v1.0/paymentchannel/registerAuth/query";
        requestEntity = new LinkedMultiValueMap<String, String>();
        requestEntity.add("mobile", telphone);
        log.info("接口/v1.0/paymentchannel/registerAuth/query--参数================" + requestEntity.toString());
        try {
            result = restTemplate.postForObject(url, requestEntity, String.class);
            log.info("接口/v1.0/paymentchannel/registerAuth/query--result================" + result.toString());
            jsonObject = JSONObject.fromObject(result);
        } catch (Exception e) {
            log.error("查询商户进件信息有误=======" + e);
            maps.put(CommonConstants.RESP_CODE, "failed");
            maps.put(CommonConstants.RESP_MESSAGE, "查询商户进件信息出错啦");
            return maps;
        }
        String bankAccountNumber = null;
        if (!"null".equals(jsonObject.getString("result")) && null != jsonObject.getString("result")
                && !"".equals(jsonObject.getString("result"))) {
            JSONObject obj = JSONObject.fromObject(jsonObject.getString("result"));
            bankAccountNumber = obj.getString("bankAccountNumber");
            log.info("============bankAccountNumber============" + bankAccountNumber);
        }
        String mainCustomerNumber = Conts.customerNumber; // 代理商编码
        String requestId = UUID.randomUUID().toString().substring(0, 15); // 注册请求号，每次请求唯一
        String customertype = "PERSON";// 企业-ENTERPRISE,个体工商户-INDIVIDUAL
        // 个人-PERSON
        String bindmobile = telphone;
        String signedname = userName;
        String linkman = "莘丽";
        String legalperson = userName;// 法人
        String minsettleamount = "1";
        String riskreserveday = "0";
        String bankaccountnumber = cardNo;
        String bankname = bankNameJJ;
        String accountname = userName;
        if (null == areano || "null".equals(areano) || "".equals(areano))
            areano = "1111";
        String areaCode = areano;
        Map map = new HashMap();
        if (null != extra && !"".equals(extra)) {
            if (!cardNo.equals(bankAccountNumber)) {
                uri = util.getServiceUrl("paymentchannel", "error url request!");
                url = uri.toString() + "/v1.0/paymentchannel/registerAuth/update";
                requestEntity = new LinkedMultiValueMap<String, String>();
                requestEntity.add("mobile", telphone);
                requestEntity.add("bank_account_number", cardNo);
                requestEntity.add("bank_name", bankname);
                log.info("接口/v1.0/paymentchannel/registerAuth/update--参数================" + requestEntity.toString());
                result = restTemplate.postForObject(url, requestEntity, String.class);
                log.info("接口/v1.0/paymentchannel/registerAuth/update--result================" + result.toString());

                PostMethod postMethod = new PostMethod("https://skb.yeepay.com/skb-app/customerInforUpdate.action");

                HttpClient client = new HttpClient();

                StringBuffer signature = new StringBuffer();
                signature.append(mainCustomerNumber == null ? "" : mainCustomerNumber)
                        .append(extra == null ? "" : extra).append(cardNo == null ? "" : cardNo)
                        .append(bankname == null ? "" : bankname);
                System.out.println("source===" + signature.toString());
                String hmac = Digest.hmacSign(signature.toString(), key);
                System.out.println("hmac====" + hmac);
                Part[] parts = new CustomerInforUpdatePartsBuilder().setMainCustomerNumber(mainCustomerNumber)
                        .setCustomerNumber(extra).setModifyType("2").setBankCardNumber(cardNo).setBankName(bankname)
                        .setHmac(hmac).generateParams();

                postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));

                int status = client.executeMethod(postMethod);
                if (status == HttpStatus.SC_OK) {
                    result = postMethod.getResponseBodyAsString();
                    JSONObject obj = JSONObject.fromObject(result);
                    System.out.println("===============");
                    System.out.println("result" + result);
                    System.out.println("===============");
                    if (obj.getString("code").equals("0000")) {
                        log.info("============修改成功============");
                    }

                }
            }

            log.info("============已注册路径============");
            PostMethod postMethod = null;
            HttpClient client = null;
            StringBuffer signature = null;
            String customerNumber = extra;
            String productType = "1";
            int status = 0;
            int statusAll = 0;
            String backinfo = null;
            String hmac = null;
            Part[] parts = null;
            for (int i = 1; i < 6; i++) {
                postMethod = new PostMethod(Conts.baseRequestUrl + "/feeSetApi.action");
                client = new HttpClient();
                signature = new StringBuffer();
                if (i == 2)
                    continue;
                if (i == 3)
                    rateCopy = extraFee;
                if (i == 4)
                    rateCopy = "0";
                if (i == 5)
                    rateCopy = "0";
                productType = String.valueOf(i);
                signature.append(customerNumber == null ? "" : customerNumber)
                        .append(mainCustomerNumber == null ? "" : mainCustomerNumber)
                        .append(productType == null ? "" : productType).append(rateCopy == null ? "" : rateCopy);
                System.out.println("source===" + signature.toString());

                hmac = Digest.hmacSign(signature.toString(), key);
                parts = new RegisterPartsBuilder().setCustomerNumber(customerNumber)
                        .setMainCustomerNumber(mainCustomerNumber).setProductType(productType).setHmac(hmac)
                        .setRate(rateCopy).generateParams();

                postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));

                status = client.executeMethod(postMethod);
                backinfo = postMethod.getResponseBodyAsString();
                log.info("==========backinfo" + i + "===========" + backinfo);
                if (status == HttpStatus.SC_OK) {
                    JSONObject obj = JSONObject.fromObject(backinfo);
                    if (obj.getString("code").equals("0000")) {
                        statusAll++;
                    }
                }
            }
            if (statusAll == 4) {
                JSONObject obj = JSONObject.fromObject(backinfo);
                if (obj.getString("code").equals("0000")) {
                    postMethod = new PostMethod(Conts.baseRequestUrl + "/receiveApi.action");
                    client = new HttpClient();
                    String source = "B";
                    String mcc = "5311";
                    String payerBankAccountNo = bankcard;
                    String mobileNumber = telphone;
                    String callBackUrl = notifyurl;
                    String webCallBackUrl = returnurl;
                    String requestcode = ordercode.substring(0, 20);
                    StringBuilder hmacStr = new StringBuilder();
                    hmacStr.append(source == null ? "" : source)
                            .append(mainCustomerNumber == null ? "" : mainCustomerNumber)
                            .append(customerNumber == null ? "" : customerNumber).append(amount == null ? "" : amount)
                            .append(mcc == null ? "" : mcc).append(requestcode == null ? "" : requestcode)
                            .append(mobileNumber == null ? "" : mobileNumber)
                            .append(callBackUrl == null ? "" : callBackUrl)
                            .append(webCallBackUrl == null ? "" : webCallBackUrl)
                            .append(payerBankAccountNo == null ? "" : payerBankAccountNo);

                    hmac = Digest.hmacSign(hmacStr.toString(), key);

                    parts = new ReceviePartsBuiler().setMainCustomerNumber(mainCustomerNumber).setAmount(amount)
                            .setCustomerNumber(customerNumber).setHamc(hmac).setMcc(mcc).setMobileNumber(mobileNumber)
                            .setCallBackUrl(callBackUrl).setRequestId(requestcode).setSource(source)
                            .setWebCallBackUrl(webCallBackUrl).setPayerBankAccountNo(payerBankAccountNo)
                            .generateParams();

                    postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));

                    status = client.executeMethod(postMethod);
                    if (status == HttpStatus.SC_OK) {
                        backinfo = postMethod.getResponseBodyAsString();
                        log.info("==========backinfo4==========" + backinfo);
                        obj = JSONObject.fromObject(backinfo);
                        if (obj.getString("code").equals("0000")) {
                            String backurl = obj.getString("url");
                            url = AESUtil.decrypt(backurl, key);
                            log.info("==========url==========" + url);
                            map.put("redirect_url", url);
                            map.put("resp_code", "success");
                            map.put("channel_type", "yb");

                        } else {
                            map.put("channel_type", "yb");
                            map.put("resp_code", "failed");
                            map.put("resp_message", "交易失败：" + obj.getString("message"));
                        }
                    } else if (status == HttpStatus.SC_MOVED_PERMANENTLY || status == HttpStatus.SC_MOVED_TEMPORARILY) {
                        // 从头中取出转向的地址
                        Header locationHeader = postMethod.getResponseHeader("location");
                        String location = null;
                        if (locationHeader != null) {
                            location = locationHeader.getValue();
                            System.out.println("The page was redirected to:" + location);
                        } else {
                            System.err.println("Location field value is null.");
                        }
                    } else {
                        System.out.println("fail======" + status);
                    }

                } else {
                    map.put("channel_type", "yb");
                    map.put("resp_code", "failed");
                    map.put("resp_message", "设置费率失败：" + obj.getString("message"));
                }
            } else if (status == HttpStatus.SC_MOVED_PERMANENTLY || status == HttpStatus.SC_MOVED_TEMPORARILY) {

                Header locationHeader = postMethod.getResponseHeader("location");
                String location = null;
                if (locationHeader != null) {
                    location = locationHeader.getValue();
                    System.out.println("The page was redirected to:" + location);
                } else {
                    System.err.println("Location field value is null.");
                }
            } else {
                map.put("channel_type", "yb");
                map.put("resp_code", "failed");
                map.put("resp_message", "设置费率失败!");
            }
            return map;

        } else {

            log.info("============未注册路径============");
//			File dir = new File(realnamePic + "/back" + telphone);
////             创建目录
//			if (dir.mkdirs()) {
//				System.out.println("创建目录" + realnamePic + "/back" + telphone + "成功！");
//				log.info("=================创建目录成功================");
//			} else {
//				File[] tempfiles = dir.listFiles();
//				for (File file : tempfiles) {
//					file.delete();
//				}
//				log.info("=================创建目录失败================");
//				System.out.println("创建目录" + realnamePic + "/back" + telphone + "失败！");
//			}
            File file = new File(realnamePic +"/"+ telphone);
            String[] filelist = file.list();
            log.info("=================fileList================"+filelist.length+"filePath"+file.getPath());
            if (filelist != null) {
                for (int i = 0; i < filelist.length; i++) {
                    Img2Small.resizeImage(realnamePic +"/"+ telphone + "/" + filelist[i],
//                            Img2Small.resizeImage("/usr/share/nginx/html/" + telphone + "/" + filelist[i],
                            realnamePic + "/" + telphone + "/" + filelist[i], 2000, 2000);
                    log.info("============================="+realnamePic + "/" + telphone + "/" + filelist[i]);
                }
            }
//			File file = new File("/product/upload/images/123" + "/" + telphone);
            file = new File(realnamePic + "/" + telphone);
            filelist = file.list();
            File f1 = new File(realnamePic + "/" + telphone + "/" + filelist[0]);
            File f2 = new File(realnamePic + "/" + telphone + "/" + filelist[1]);
            File f3 = new File(realnamePic + "/" + telphone + "/" + filelist[2]);
            PostMethod postMethod = new PostMethod(Conts.baseRequestUrl + "/register.action");
            // PostMethod postMethod = new
            // PostMethod("http://10.151.30.4:8057/skb-app/register.action");
            HttpClient client = new HttpClient();
            String manualSettle = "Y";
            try {
                StringBuffer signature = new StringBuffer();
                signature.append(mainCustomerNumber == null ? "" : mainCustomerNumber)
                        .append(requestId == null ? "" : requestId).append(customertype == null ? "" : customertype)
                        .append(bindmobile == null ? "" : bindmobile).append(signedname == null ? "" : signedname)
                        .append(linkman == null ? "" : linkman).append(idcard == null ? "" : idcard)
                        .append(legalperson == null ? "" : legalperson)
                        .append(minsettleamount == null ? "" : minsettleamount)
                        .append(riskreserveday == null ? "" : riskreserveday)
                        .append(bankaccountnumber == null ? "" : bankaccountnumber)
                        .append(bankname == null ? "" : bankname).append(accountname == null ? "" : accountname)
                        .append(areaCode == null ? "" : areaCode).append(manualSettle == null ? "" : manualSettle);

                System.out.println("signature=======================" + signature.toString());
                String hmac = Digest.hmacSign(signature.toString(), key);
                System.out.println("hmac=======================" + hmac);

                Part[] parts = new RegisterPartsBuilder().setMainCustomerNumber(mainCustomerNumber)
                        .setRequestId(requestId).setCustomerType(customertype).setBindMobile(bindmobile)
                        .setSignedName(signedname).setLinkMan(linkman).setIdCard(idcard).setLegalPerson(legalperson)
                        .setMinSettleAmount(minsettleamount).setRiskReserveDay(riskreserveday)
                        .setBankAccountNumber(bankaccountnumber).setBankName(bankname).setAccountName(accountname)
                        .setAreaCode(areaCode).setManualSettle(manualSettle).setBankCardPhoto(f1).setIdCardBackPhoto(f2)
                        .setIdCardPhoto(f1).setPersonPhoto(f3).setHmac(hmac).generateParams();

                postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
                System.out.println(postMethod.toString());

                int status = client.executeMethod(postMethod);
                log.info("==========status==========" + status);

                if (status == HttpStatus.SC_OK) {
                    String backinfo = postMethod.getResponseBodyAsString();
                    log.info("==========postMethod.getResponseBodyAsString()==========" + backinfo);
                    JSONObject obj = JSONObject.fromObject(backinfo);
                    if (obj.getString("code").equals("0000")) {
                        String customerNumber = obj.getString("customerNumber");

                        uri = util.getServiceUrl("paymentchannel", "error url request!");
                        road = uri.toString() + "/v1.0/paymentchannel/registerAuth/save";
                        /** 根据的渠道标识或去渠道的相关信息 */
                        requestEntity = new LinkedMultiValueMap<String, String>();
                        requestEntity.add("request_id", requestId);
                        requestEntity.add("mobile", telphone);
                        requestEntity.add("id_card", idcard);
                        requestEntity.add("legal_person", userName);
                        requestEntity.add("min_settle_amoun", minsettleamount);
                        requestEntity.add("risk_reserve_day", riskreserveday);
                        requestEntity.add("bank_account_number", bankaccountnumber);
                        requestEntity.add("bank_name", bankname);
                        requestEntity.add("customer_number", customerNumber);
                        requestEntity.add("status", "0");// 0成功
                        requestEntity.add("rate", rate);
                        requestEntity.add("charge", extraFee);
                        restTemplate = new RestTemplate();
                        log.info("接口/v1.0/paymentchannel/registerAuth/save--参数================"
                                + restTemplate.toString());
                        resultObjx = restTemplate.postForObject(road, requestEntity, String.class);
                        log.info("接口/v1.0/paymentchannel/registerAuth/save--RESULT================" + resultObjx);

                        String stat = "SUCCESS";
                        postMethod = new PostMethod(Conts.baseRequestUrl + "/auditMerchant.action");
                        client = new HttpClient();
                        StringBuilder hmacStr = new StringBuilder();
                        hmacStr.append(customerNumber == null ? "" : customerNumber)
                                .append(mainCustomerNumber == null ? "" : mainCustomerNumber)
                                .append(stat == null ? "" : stat);
                        hmac = Digest.hmacSign(hmacStr.toString(), key);
                        Part[] parts2 = new AuditMerchantPartsBuilder().setMainCustomerNumber(mainCustomerNumber)
                                .setCustomerNumber(customerNumber).setStatus(stat).setHmac(hmac).generateParams();

                        postMethod.setRequestEntity(new MultipartRequestEntity(parts2, postMethod.getParams()));

                        status = client.executeMethod(postMethod);
                        log.info("==========status==========" + status);
                        if (status == HttpStatus.SC_OK) {
                            backinfo = postMethod.getResponseBodyAsString();
                            log.info("==========backinfo2==========" + backinfo);
                            obj = JSONObject.fromObject(backinfo);
                            if (obj.getString("code").equals("0000")) {
                                int statusAll = 0;
                                for (int i = 1; i < 6; i++) {
                                    postMethod = new PostMethod(Conts.baseRequestUrl + "/feeSetApi.action");
                                    client = new HttpClient();
                                    signature = new StringBuffer();
                                    if (i == 2)
                                        continue;
                                    if (i == 3)
                                        rateCopy = extraFee;
                                    if (i == 4)
                                        rateCopy = "0";
                                    if (i == 5)
                                        rateCopy = "0";
                                    String productType = String.valueOf(i);
                                    signature.append(customerNumber == null ? "" : customerNumber)
                                            .append(mainCustomerNumber == null ? "" : mainCustomerNumber)
                                            .append(productType == null ? "" : productType)
                                            .append(rateCopy == null ? "" : rateCopy);
                                    System.out.println("source===" + signature.toString());

                                    hmac = Digest.hmacSign(signature.toString(), key);
                                    parts = new RegisterPartsBuilder().setCustomerNumber(customerNumber)
                                            .setMainCustomerNumber(mainCustomerNumber).setProductType(productType)
                                            .setHmac(hmac).setRate(rateCopy).generateParams();

                                    postMethod.setRequestEntity(
                                            new MultipartRequestEntity(parts, postMethod.getParams()));

                                    status = client.executeMethod(postMethod);
                                    log.info("==========backinfo" + i + "===========" + backinfo);
                                    if (status == HttpStatus.SC_OK) {
                                        backinfo = postMethod.getResponseBodyAsString();
                                        obj = JSONObject.fromObject(backinfo);
                                        if (obj.getString("code").equals("0000")) {
                                            statusAll++;
                                        }
                                    }
                                }
                                if (statusAll == 4) {
                                    backinfo = postMethod.getResponseBodyAsString();
                                    log.info("==========backinfo3==========" + backinfo);
                                    obj = JSONObject.fromObject(backinfo);
                                    if (obj.getString("code").equals("0000")) {
                                        postMethod = new PostMethod(Conts.baseRequestUrl + "/receiveApi.action");
                                        client = new HttpClient();
                                        String source = "B";
                                        String mcc = "5311";
                                        String payerBankAccountNo = bankcard;
                                        String mobileNumber = telphone;
                                        String callBackUrl = notifyurl;
                                        String webCallBackUrl = returnurl;
                                        String requestcode = ordercode.substring(0, 20);
                                        hmacStr = new StringBuilder();
                                        hmacStr.append(source == null ? "" : source)
                                                .append(mainCustomerNumber == null ? "" : mainCustomerNumber)
                                                .append(customerNumber == null ? "" : customerNumber)
                                                .append(amount == null ? "" : amount).append(mcc == null ? "" : mcc)
                                                .append(requestcode == null ? "" : requestcode)
                                                .append(mobileNumber == null ? "" : mobileNumber)
                                                .append(callBackUrl == null ? "" : callBackUrl)
                                                .append(webCallBackUrl == null ? "" : webCallBackUrl)
                                                .append(payerBankAccountNo == null ? "" : payerBankAccountNo);

                                        hmac = Digest.hmacSign(hmacStr.toString(), key);

                                        parts = new ReceviePartsBuiler().setMainCustomerNumber(mainCustomerNumber)
                                                .setAmount(amount).setCustomerNumber(customerNumber).setHamc(hmac)
                                                .setMcc(mcc).setMobileNumber(mobileNumber).setCallBackUrl(callBackUrl)
                                                .setRequestId(requestcode).setSource(source)
                                                .setWebCallBackUrl(webCallBackUrl)
                                                .setPayerBankAccountNo(payerBankAccountNo).generateParams();

                                        postMethod.setRequestEntity(
                                                new MultipartRequestEntity(parts, postMethod.getParams()));

                                        status = client.executeMethod(postMethod);
                                        if (status == HttpStatus.SC_OK) {
                                            backinfo = postMethod.getResponseBodyAsString();
                                            log.info("==========backinfo4==========" + backinfo);
                                            obj = JSONObject.fromObject(backinfo);
                                            if (obj.getString("code").equals("0000")) {
                                                String backurl = obj.getString("url");
                                                url = AESUtil.decrypt(backurl, key);
                                                log.info("==========url==========" + url);
                                                map.put("redirect_url", url);
                                                map.put("resp_code", "success");
                                                map.put("channel_type", "yb");

                                            } else {
                                                map.put("channel_type", "yb");
                                                map.put("resp_code", "failed");
                                                map.put("resp_message", "交易失败：" + obj.getString("message"));
                                            }
                                        } else if (status == HttpStatus.SC_MOVED_PERMANENTLY
                                                || status == HttpStatus.SC_MOVED_TEMPORARILY) {
                                            // 从头中取出转向的地址
                                            Header locationHeader = postMethod.getResponseHeader("location");
                                            String location = null;
                                            if (locationHeader != null) {
                                                location = locationHeader.getValue();
                                                System.out.println("The page was redirected to:" + location);
                                            } else {
                                                System.err.println("Location field value is null.");
                                            }
                                        } else {
                                            System.out.println("fail======" + status);
                                        }

                                    } else {
                                        map.put("channel_type", "yb");
                                        map.put("resp_code", "failed");
                                        map.put("resp_message", "设置费率失败：" + obj.getString("message"));
                                    }
                                } else if (status == HttpStatus.SC_MOVED_PERMANENTLY
                                        || status == HttpStatus.SC_MOVED_TEMPORARILY) {

                                    Header locationHeader = postMethod.getResponseHeader("location");
                                    String location = null;
                                    if (locationHeader != null) {
                                        location = locationHeader.getValue();
                                        System.out.println("The page was redirected to:" + location);
                                    } else {
                                        System.err.println("Location field value is null.");
                                    }
                                } else {
                                    map.put("channel_type", "yb");
                                    map.put("resp_code", "failed");
                                    map.put("resp_message", "设置费率失败：" + obj.getString("message"));
                                }
                            } else {
                                map.put("channel_type", "yb");
                                map.put("resp_code", "failed");
                                map.put("resp_message", "审核失败：" + obj.getString("message"));
                            }

                        } else if (status == HttpStatus.SC_MOVED_PERMANENTLY
                                || status == HttpStatus.SC_MOVED_TEMPORARILY) {

                            Header locationHeader = postMethod.getResponseHeader("location");
                            String location = null;
                            if (locationHeader != null) {
                                location = locationHeader.getValue();
                                System.out.println("The page was redirected to:" + location);
                            } else {
                                System.err.println("Location field value is null.");
                            }
                        } else {
                            System.out.println("fail======" + status);
                        }
                    } else {
                        map.put("channel_type", "yb");
                        map.put("resp_code", "failed");
                        map.put("resp_message", "注册失败：" + obj.getString("message"));
                    }
                } else if (status == HttpStatus.SC_MOVED_PERMANENTLY || status == HttpStatus.SC_MOVED_TEMPORARILY) {
                    // 从头中取出转向的地址
                    Header locationHeader = postMethod.getResponseHeader("location");
                    String location = null;
                    if (locationHeader != null) {
                        location = locationHeader.getValue();
                        System.out.println("The page was redirected to:" + location);
                    } else {
                        System.err.println("Location field value is null.");
                    }
                } else {
                    map.put("channel_type", "yb");
                    map.put("resp_code", "failed");
                    map.put("resp_message", "失败码：" + status);
                }
            } catch (Exception e) {
                map.put("channel_type", "yb");
                map.put("resp_code", "failed");
                map.put("resp_message", "失败信息：" + e.getMessage());
                e.printStackTrace();
            } finally {
                // 释放连接
                postMethod.releaseConnection();
                return map;
            }
        }

    }

    /**
     * @param result
     */
    private static void parseResult(String result) throws  Exception{

        JSONObject jsonResult = JSONObject.fromObject(result);

        String url = jsonResult.getString("url");

        url = AESUtil.decrypt(url, key);

        System.out.println("===============");
        System.out.println("url = " + url);
        System.out.println("===============");
    }
}

class RegisterPartsBuilder {

    private List<Part> parts = new ArrayList<Part>(31);

    public Part[] generateParams() {
        return parts.toArray(new Part[parts.size()]);
    }

    public RegisterPartsBuilder setCustomerNumber(String customerNumber) {
        this.parts.add(new StringPart("customerNumber", customerNumber == null ? "" : customerNumber, "UTF-8"));
        return this;
    }

    public RegisterPartsBuilder setProductType(String productType) {
        this.parts.add(new StringPart("productType", productType == null ? "" : productType, "UTF-8"));
        return this;
    }

    public RegisterPartsBuilder setRate(String rate) {
        this.parts.add(new StringPart("rate", rate == null ? "" : rate, "UTF-8"));
        return this;
    }

    /**
     * @param mainCustomerNumber the mainCustomerNumber to set
     */
    public RegisterPartsBuilder setMainCustomerNumber(String mainCustomerNumber) {
        this.parts.add(
                new StringPart("mainCustomerNumber", mainCustomerNumber == null ? "" : mainCustomerNumber, "UTF-8"));
        return this;
    }

    /**
     * @param bindMobile the bindMobile to set
     */
    public RegisterPartsBuilder setBindMobile(String bindMobile) {
        this.parts.add(new StringPart("bindMobile", bindMobile == null ? "" : bindMobile, "UTF-8"));
        return this;
    }

    /**
     * @param signedName the signedName to set
     */
    public RegisterPartsBuilder setSignedName(String signedName) {
        this.parts.add(new StringPart("signedName", signedName == null ? "" : signedName, "UTF-8"));
        return this;
    }

    /**
     * @param linkMan the linkMan to set
     */
    public RegisterPartsBuilder setLinkMan(String linkMan) {
        this.parts.add(new StringPart("linkMan", linkMan == null ? "" : linkMan, "UTF-8"));
        return this;
    }

    /**
     * @param idCard the idCard to set
     */
    public RegisterPartsBuilder setIdCard(String idCard) {
        this.parts.add(new StringPart("idCard", idCard == null ? "" : idCard, "UTF-8"));
        return this;
    }

    /**
     * @param legalPerson the legalPerson to set
     */
    public RegisterPartsBuilder setLegalPerson(String legalPerson) {
        this.parts.add(new StringPart("legalPerson", legalPerson == null ? "" : legalPerson, "UTF-8"));
        return this;
    }

    /**
     * @param minSettleAmount the minSettleAmount to set
     */
    public RegisterPartsBuilder setMinSettleAmount(String minSettleAmount) {
        this.parts.add(new StringPart("minSettleAmount", minSettleAmount == null ? "" : minSettleAmount, "UTF-8"));
        return this;
    }

    /**
     * @param riskReserveDay the riskReserveDay to set
     */
    public RegisterPartsBuilder setRiskReserveDay(String riskReserveDay) {
        this.parts.add(new StringPart("riskReserveDay", riskReserveDay == null ? "" : riskReserveDay, "UTF-8"));
        return this;
    }

    /**
     * @param bankAccountNumber the bankAccountNumber to set
     */
    public RegisterPartsBuilder setBankAccountNumber(String bankAccountNumber) {
        this.parts
                .add(new StringPart("bankAccountNumber", bankAccountNumber == null ? "" : bankAccountNumber, "UTF-8"));
        return this;
    }

    /**
     * @param bankName the bankName to set
     */
    public RegisterPartsBuilder setBankName(String bankName) {

        this.parts.add(new StringPart("bankName", bankName == null ? "" : bankName, "UTF-8"));
        return this;
    }

    /**
     * @param accountName the cacountName to set
     */
    public RegisterPartsBuilder setAccountName(String accountName) {
        this.parts.add(new StringPart("accountName", accountName == null ? "" : accountName, "UTF-8"));
        return this;
    }

    /**
     * @param requestId the requestId to set
     */
    public RegisterPartsBuilder setRequestId(String requestId) {
        this.parts.add(new StringPart("requestId", requestId == null ? "" : requestId, "UTF-8"));
        return this;
    }

    /**
     * @param customerType the customerType to set
     */
    public RegisterPartsBuilder setCustomerType(String customerType) {
        this.parts.add(new StringPart("customerType", customerType == null ? "" : customerType, "UTF-8"));
        return this;
    }

    /**
     * @param hmac the hmac to set
     */
    public RegisterPartsBuilder setHmac(String hmac) {
        this.parts.add(new StringPart("hmac", hmac == null ? "" : hmac, "UTF-8"));
        return this;
    }

    /**
     * @param areaCode the areaCode to set
     */
    public RegisterPartsBuilder setAreaCode(String areaCode) {
        this.parts.add(new StringPart("areaCode", areaCode == null ? "" : areaCode, "UTF-8"));
        return this;
    }

    /**
     * setter
     *
     * @param mailstr
     */
    public RegisterPartsBuilder setMailStr(String mailstr) {
        this.parts.add(new StringPart("mailstr", mailstr == null ? "" : mailstr, "UTF-8"));
        return this;
    }

    /**
     * setter
     *
     * @param loginpassword
     */
    public RegisterPartsBuilder setLoginPassword(String loginpassword) {
        this.parts.add(new StringPart("loginPassword", loginpassword == null ? "" : loginpassword, "UTF-8"));
        return this;
    }

    /**
     * setter
     *
     * @param tradepassword
     */
    public RegisterPartsBuilder setTradePassword(String tradepassword) {
        this.parts.add(new StringPart("tradePassword", tradepassword == null ? "" : tradepassword, "UTF-8"));
        return this;
    }

    public RegisterPartsBuilder setBankaccounttype(String bankaccounttype) {
        this.parts.add(new StringPart("bankAccountType", bankaccounttype == null ? "" : bankaccounttype, "UTF-8"));
        return this;
    }

    /**
     * @param businessLicence the businessLicence to set
     */
    public RegisterPartsBuilder setBusinessLicence(String businessLicence) {
        this.parts.add(new StringPart("businessLicence", businessLicence == null ? "" : businessLicence, "UTF-8"));
        return this;
    }

    /**
     * @param loginPasswordConfirm the loginPasswordConfirm to set
     */
    public RegisterPartsBuilder setLoginPasswordConfirm(String loginPasswordConfirm) {
        this.parts.add(new StringPart("loginPasswordConfirm", loginPasswordConfirm == null ? "" : loginPasswordConfirm,
                "UTF-8"));
        return this;
    }

    /**
     * @param tradePasswordConfirm the tradePasswordConfirm to set
     */
    public RegisterPartsBuilder setTradePasswordConfirm(String tradePasswordConfirm) {
        this.parts.add(new StringPart("tradePasswordConfirm", tradePasswordConfirm == null ? "" : tradePasswordConfirm,
                "UTF-8"));
        return this;
    }

    /**
     * 分润方
     *
     * @param splitter the splitter to set
     */
    public RegisterPartsBuilder setSplitter(String splitter) {
        this.parts.add(new StringPart("splitter", splitter == null ? "" : splitter, "UTF-8"));
        return this;
    }

    /**
     * 白名单
     */
    public RegisterPartsBuilder setWhiteList(String whiteList) {
        this.parts.add(new StringPart("whiteList", whiteList == null ? "" : whiteList, "UTF-8"));
        return this;
    }

    /***/
    public RegisterPartsBuilder setFreezeDays(String freezeDays) {
        this.parts.add(new StringPart("freezeDays", freezeDays == null ? "" : freezeDays, "UTF-8"));
        return this;
    }

    /**
     * 分润比率
     *
     * @param splitterprofitfee the splitterprofitfee to set
     */
    public RegisterPartsBuilder setSplitterprofitfee(String splitterprofitfee) {
        this.parts
                .add(new StringPart("splitterprofitfee", splitterprofitfee == null ? "" : splitterprofitfee, "UTF-8"));
        return this;
    }

    // [end] jun.lin 2015-03-30 这里是普通入参

    // [start] jun.lin 2015-03-20 这里是文件入参

    private void configFilePart(File f, FilePart fp) {
        String fileName = f.getName();
        fp.setContentType("image/" + fileName.substring(fileName.lastIndexOf('.') + 1));
        fp.setCharSet("UTF-8");

        System.out.println(fp.getContentType());
    }

    private void configPdfFilePart(File f, FilePart fp) {
        String fileName = f.getName();
        fp.setContentType("application/" + fileName.substring(fileName.lastIndexOf('.') + 1));
        fp.setCharSet("UTF-8");

        System.out.println(fp.getContentType());
    }

    /**
     * @param idCardPhoto the idCardPhoto to set
     * @throws FileNotFoundException
     */
    public RegisterPartsBuilder setIdCardPhoto(File idCardPhoto) throws FileNotFoundException {
        FilePart fp = new FilePart("idCardPhoto", idCardPhoto);
        configFilePart(idCardPhoto, fp);
        this.parts.add(fp);

        return this;
    }

    // 目前非必传
    public RegisterPartsBuilder setIdCardBackPhoto(File idCardBackPhoto) throws FileNotFoundException {
        FilePart fp = new FilePart("idCardBackPhoto", idCardBackPhoto);
        configFilePart(idCardBackPhoto, fp);
        this.parts.add(fp);

        return this;
    }

    /**
     * @param bankCardPhoto the bankCardPhoto to set
     * @throws FileNotFoundException
     */
    public RegisterPartsBuilder setBankCardPhoto(File bankCardPhoto) throws FileNotFoundException {
        FilePart fp = new FilePart("bankCardPhoto", bankCardPhoto);
        configFilePart(bankCardPhoto, fp);
        this.parts.add(fp);
        return this;
    }

    /**
     * @param personPhoto the personPhoto to set
     * @throws FileNotFoundException
     */
    public RegisterPartsBuilder setPersonPhoto(File personPhoto) throws FileNotFoundException {
        FilePart fp = new FilePart("personPhoto", personPhoto);
        configFilePart(personPhoto, fp);
        this.parts.add(fp);
        return this;
    }

    /**
     * @param businessLicensePhoto the businessLicensePhoto to set
     * @throws FileNotFoundException
     */
    public RegisterPartsBuilder setBusinessLicensePhoto(File businessLicensePhoto) throws FileNotFoundException {
        FilePart fp = new FilePart("businessLicensePhoto", businessLicensePhoto);
        configFilePart(businessLicensePhoto, fp);
        this.parts.add(fp);
        return this;
    }

    public RegisterPartsBuilder setCertFee(String certFee) {
        this.parts.add(new StringPart("certFee", certFee == null ? "" : certFee, "UTF-8"));
        return this;
    }

    public RegisterPartsBuilder setManualSettle(String manualSettle) {
        this.parts.add(new StringPart("manualSettle", manualSettle == null ? "" : manualSettle, "UTF-8"));
        return this;
    }

    public RegisterPartsBuilder setElectronicAgreement(File electronicAgreement) throws FileNotFoundException {
        FilePart fp = new FilePart("electronicAgreement", electronicAgreement);
        configPdfFilePart(electronicAgreement, fp);
        this.parts.add(fp);
        return this;
    }
}

class AuditMerchantPartsBuilder {

    private List<Part> parts = new ArrayList<Part>(11);

    public Part[] generateParams() {
        return parts.toArray(new Part[parts.size()]);
    }

    public AuditMerchantPartsBuilder setStatus(String status) {
        this.parts.add(new StringPart("status", status == null ? "" : status, "UTF-8"));
        return this;
    }

    public AuditMerchantPartsBuilder setMainCustomerNumber(String mainCustomerNumber) {
        this.parts.add(
                new StringPart("mainCustomerNumber", mainCustomerNumber == null ? "" : mainCustomerNumber, "UTF-8"));
        return this;
    }

    public AuditMerchantPartsBuilder setCustomerNumber(String customerNumber) {
        this.parts.add(new StringPart("customerNumber", customerNumber == null ? "" : customerNumber, "UTF-8"));
        return this;
    }

    public AuditMerchantPartsBuilder setHmac(String hmac) {
        this.parts.add(new StringPart("hmac", hmac == null ? "" : hmac, "UTF-8"));
        return this;
    }

    public AuditMerchantPartsBuilder setReason(String reason) {
        this.parts.add(new StringPart("reason", reason == null ? "" : reason, "UTF-8"));
        return this;
    }
}

class ReceviePartsBuiler {

    private List<Part> parts = new ArrayList<Part>(11);

    public Part[] generateParams() {
        return parts.toArray(new Part[parts.size()]);
    }

    public ReceviePartsBuiler setSource(String source) {
        this.parts.add(new StringPart("source", source == null ? "" : source, "UTF-8"));
        return this;
    }

    public ReceviePartsBuiler setMainCustomerNumber(String mainCustomerNumber) {
        this.parts.add(
                new StringPart("mainCustomerNumber", mainCustomerNumber == null ? "" : mainCustomerNumber, "UTF-8"));
        return this;
    }

    public ReceviePartsBuiler setCustomerNumber(String customerNumber) {
        this.parts.add(new StringPart("customerNumber", customerNumber == null ? "" : customerNumber, "UTF-8"));
        return this;
    }

    public ReceviePartsBuiler setRequestId(String requestId) {
        this.parts.add(new StringPart("requestId", requestId == null ? "" : requestId, "UTF-8"));
        return this;
    }

    public ReceviePartsBuiler setAmount(String amout) {
        this.parts.add(new StringPart("amount", amout == null ? "" : amout, "UTF-8"));
        return this;
    }

    public ReceviePartsBuiler setMcc(String mcc) {
        this.parts.add(new StringPart("mcc", mcc == null ? "" : mcc, "UTF-8"));
        return this;
    }

    public ReceviePartsBuiler setMobileNumber(String mobileNumber) {
        this.parts.add(new StringPart("mobileNumber", mobileNumber == null ? "" : mobileNumber, "UTF-8"));
        return this;
    }

    public ReceviePartsBuiler setCallBackUrl(String callBackUrl) {
        this.parts.add(new StringPart("callBackUrl", callBackUrl == null ? "" : callBackUrl, "UTF-8"));
        return this;
    }

    public ReceviePartsBuiler setWebCallBackUrl(String webCallBackUrl) {
        this.parts.add(new StringPart("webCallBackUrl", webCallBackUrl == null ? "" : webCallBackUrl, "UTF-8"));
        return this;
    }

    public ReceviePartsBuiler setSmgCallBackUrl(String smgCallBackUrl) {
        this.parts.add(new StringPart("smgCallBackUrl", smgCallBackUrl == null ? "" : smgCallBackUrl, "UTF-8"));
        return this;
    }

    public ReceviePartsBuiler setPayerBankAccountNo(String payerBankAccountNo) {
        this.parts.add(
                new StringPart("payerBankAccountNo", payerBankAccountNo == null ? "" : payerBankAccountNo, "UTF-8"));
        return this;
    }

    public ReceviePartsBuiler setHamc(String hmac) {
        this.parts.add(new StringPart("hmac", hmac == null ? "" : hmac, "UTF-8"));
        return this;
    }

    public ReceviePartsBuiler setCfca(String cfca) {
        this.parts.add(new StringPart("cfca", cfca == null ? "" : cfca, "UTF-8"));
        return this;
    }

    public ReceviePartsBuiler setDescription(String description) {
        this.parts.add(new StringPart("description", description == null ? "" : description, "UTF-8"));
        return this;
    }

}
